﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gmis.IDAL;
using Gmis.DBUtility;

namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:PActivist
    /// </summary>
    public partial class PActivist : IPActivist
    {
        public PActivist()
        { }

        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from PActivist");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gmis.Model.PActivist model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into PActivist(");
            strSql.Append("sId,sNumber,sName,sSex,nAge,sEducation,sWorkPlace,sPosition,dJoinWork,dApply,dFoster,sContact1,sContact2,nPVotes,nVVotes)");
            strSql.Append(" values (");
            strSql.Append("@sId,@sNumber,@sName,@sSex,@nAge,@sEducation,@sWorkPlace,@sPosition,@dJoinWork,@dApply,@dFoster,@sContact1,@sContact2,@nPVotes,@nVVotes)");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@sName", SqlDbType.NVarChar,50),
					new SqlParameter("@sSex", SqlDbType.Char,4),
					new SqlParameter("@nAge", SqlDbType.Int,4),
					new SqlParameter("@sEducation", SqlDbType.NVarChar,50),
					new SqlParameter("@sWorkPlace", SqlDbType.NVarChar,128),
					new SqlParameter("@sPosition", SqlDbType.NVarChar,50),
					new SqlParameter("@dJoinWork", SqlDbType.DateTime),
					new SqlParameter("@dApply", SqlDbType.DateTime),
					new SqlParameter("@dFoster", SqlDbType.DateTime),
					new SqlParameter("@sContact1", SqlDbType.NVarChar,50),
					new SqlParameter("@sContact2", SqlDbType.NVarChar,50),
					new SqlParameter("@nPVotes", SqlDbType.Decimal,9),
					new SqlParameter("@nVVotes", SqlDbType.Decimal,9)};
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sNumber;
            parameters[2].Value = model.sName;
            parameters[3].Value = model.sSex;
            parameters[4].Value = model.nAge;
            parameters[5].Value = model.sEducation;
            parameters[6].Value = model.sWorkPlace;
            parameters[7].Value = model.sPosition;
            parameters[8].Value = model.dJoinWork;
            parameters[9].Value = model.dApply;
            parameters[10].Value = model.dFoster;
            parameters[11].Value = model.sContact1;
            parameters[12].Value = model.sContact2;
            parameters[13].Value = model.nPVotes;
            parameters[14].Value = model.nVVotes;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.PActivist model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PActivist set ");
            strSql.Append("sNumber=@sNumber,");
            strSql.Append("sName=@sName,");
            strSql.Append("sSex=@sSex,");
            strSql.Append("nAge=@nAge,");
            strSql.Append("sEducation=@sEducation,");
            strSql.Append("sWorkPlace=@sWorkPlace,");
            strSql.Append("sPosition=@sPosition,");
            strSql.Append("dJoinWork=@dJoinWork,");
            strSql.Append("dApply=@dApply,");
            strSql.Append("dFoster=@dFoster,");
            strSql.Append("sContact1=@sContact1,");
            strSql.Append("sContact2=@sContact2,");
            strSql.Append("nPVotes=@nPVotes,");
            strSql.Append("nVVotes=@nVVotes");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@sName", SqlDbType.NVarChar,50),
					new SqlParameter("@sSex", SqlDbType.Char,4),
					new SqlParameter("@nAge", SqlDbType.Int,4),
					new SqlParameter("@sEducation", SqlDbType.NVarChar,50),
					new SqlParameter("@sWorkPlace", SqlDbType.NVarChar,128),
					new SqlParameter("@sPosition", SqlDbType.NVarChar,50),
					new SqlParameter("@dJoinWork", SqlDbType.DateTime),
					new SqlParameter("@dApply", SqlDbType.DateTime),
					new SqlParameter("@dFoster", SqlDbType.DateTime),
					new SqlParameter("@sContact1", SqlDbType.NVarChar,50),
					new SqlParameter("@sContact2", SqlDbType.NVarChar,50),
					new SqlParameter("@nPVotes", SqlDbType.Decimal,9),
					new SqlParameter("@nVVotes", SqlDbType.Decimal,9),
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = model.sNumber;
            parameters[1].Value = model.sName;
            parameters[2].Value = model.sSex;
            parameters[3].Value = model.nAge;
            parameters[4].Value = model.sEducation;
            parameters[5].Value = model.sWorkPlace;
            parameters[6].Value = model.sPosition;
            parameters[7].Value = model.dJoinWork;
            parameters[8].Value = model.dApply;
            parameters[9].Value = model.dFoster;
            parameters[10].Value = model.sContact1;
            parameters[11].Value = model.sContact2;
            parameters[12].Value = model.nPVotes;
            parameters[13].Value = model.nVVotes;
            parameters[14].Value = model.sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PActivist ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PActivist ");
            strSql.Append(" where sId in (" + sIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.PActivist GetModel(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 sId,sNumber,sName,sSex,nAge,sEducation,sWorkPlace,sPosition,dJoinWork,dApply,dFoster,sContact1,sContact2,nPVotes,nVVotes from PActivist ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            Gmis.Model.PActivist model = new Gmis.Model.PActivist();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sId"] != null && ds.Tables[0].Rows[0]["sId"].ToString() != "")
                {
                    model.sId = ds.Tables[0].Rows[0]["sId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sNumber"] != null && ds.Tables[0].Rows[0]["sNumber"].ToString() != "")
                {
                    model.sNumber = ds.Tables[0].Rows[0]["sNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sName"] != null && ds.Tables[0].Rows[0]["sName"].ToString() != "")
                {
                    model.sName = ds.Tables[0].Rows[0]["sName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sSex"] != null && ds.Tables[0].Rows[0]["sSex"].ToString() != "")
                {
                    model.sSex = ds.Tables[0].Rows[0]["sSex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nAge"] != null && ds.Tables[0].Rows[0]["nAge"].ToString() != "")
                {
                    model.nAge = int.Parse(ds.Tables[0].Rows[0]["nAge"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sEducation"] != null && ds.Tables[0].Rows[0]["sEducation"].ToString() != "")
                {
                    model.sEducation = ds.Tables[0].Rows[0]["sEducation"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sWorkPlace"] != null && ds.Tables[0].Rows[0]["sWorkPlace"].ToString() != "")
                {
                    model.sWorkPlace = ds.Tables[0].Rows[0]["sWorkPlace"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPosition"] != null && ds.Tables[0].Rows[0]["sPosition"].ToString() != "")
                {
                    model.sPosition = ds.Tables[0].Rows[0]["sPosition"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dJoinWork"] != null && ds.Tables[0].Rows[0]["dJoinWork"].ToString() != "")
                {
                    model.dJoinWork = DateTime.Parse(ds.Tables[0].Rows[0]["dJoinWork"].ToString());
                }
                if (ds.Tables[0].Rows[0]["dApply"] != null && ds.Tables[0].Rows[0]["dApply"].ToString() != "")
                {
                    model.dApply = DateTime.Parse(ds.Tables[0].Rows[0]["dApply"].ToString());
                }
                if (ds.Tables[0].Rows[0]["dFoster"] != null && ds.Tables[0].Rows[0]["dFoster"].ToString() != "")
                {
                    model.dFoster = DateTime.Parse(ds.Tables[0].Rows[0]["dFoster"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sContact1"] != null && ds.Tables[0].Rows[0]["sContact1"].ToString() != "")
                {
                    model.sContact1 = ds.Tables[0].Rows[0]["sContact1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sContact2"] != null && ds.Tables[0].Rows[0]["sContact2"].ToString() != "")
                {
                    model.sContact2 = ds.Tables[0].Rows[0]["sContact2"].ToString();
                }
                if (ds.Tables[0].Rows[0]["nPVotes"] != null && ds.Tables[0].Rows[0]["nPVotes"].ToString() != "")
                {
                    model.nPVotes = decimal.Parse(ds.Tables[0].Rows[0]["nPVotes"].ToString());
                }
                if (ds.Tables[0].Rows[0]["nVVotes"] != null && ds.Tables[0].Rows[0]["nVVotes"].ToString() != "")
                {
                    model.nVVotes = decimal.Parse(ds.Tables[0].Rows[0]["nVVotes"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select sId,sNumber,sName,sSex,nAge,sEducation,sWorkPlace,sPosition,dJoinWork,dApply,dFoster,sContact1,sContact2,nPVotes,nVVotes ");
            strSql.Append(" FROM PActivist ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" sId,sNumber,sName,sSex,nAge,sEducation,sWorkPlace,sPosition,dJoinWork,dApply,dFoster,sContact1,sContact2,nPVotes,nVVotes ");
            strSql.Append(" FROM PActivist ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "PActivist";
            parameters[1].Value = "sId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

